<template>
  <div>
      <el-upload
        class="upload-demo"
        ref="upload"
        :action="hrefUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-error="handleError"
        :on-progress="handleProgress"
        :on-change="handleChange"
        :on-success="successUpload"
        :before-upload="handleBefore"
        :file-list="fileList"
        :limit="limitUpload"
        :auto-upload="aotuUpload"
        :data="paramsData"
        :on-exceed="handleExceed"
        :listType="listType"
        :http-request="upLoadFn"
        :accept="acceptUpload"
        >
          <i class="el-icon-plus" v-if="iconFlag"></i>
          <slot></slot>
      </el-upload>
  </div>
</template>

<script>
import * as API from '@/api/common/index.js'

export default {
  components: {
        
  },
  props:{
      listType:{
        type:String,
        default:"picture-card"
      },
      iconFlag:{
				type:Boolean,
				default:true
			},
      acceptUpload:{  //上传文件格式,默认xlsx
         type:String,
         default:"image/jpeg,image/png,image/jpg"
      },
      hrefUpload:{
          type:String,
          default:"https://jsonplaceholder.typicode.com/posts/"
      },
      tipMessage:{
          type:String,
          default:"请上传图片"
      },
      limitUpload:{          //可上传个数
          type:Number,
          default:1,
      },
      aotuUpload:{       //是否自动上传服务器
          type:Boolean,
          default:false,
      },
      fileListPro:{       //默认已有的图片数据,现阶段只处理一张图片或者一个文件
           type:String,
           default:""
      },
      fileName:{
           type:String,
           default:""
      },
      successFn:{         //上传成功后回调父组件的方法
          type:String,
          default:""
      },
      disabledFlag:{             //Flag为false代表新增操作,true代表删除操作
          type:Boolean,
          default:false,
      }
  },
  data() {
      return {
        paramsData:{},
        fileList:[],
      };
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {


  },
  // 监控data中的数据变化
  watch: {

  },
  methods: {
    handleError(file){},
    upLoadFn(file,fileList) {            //自定义提交方法
      let that=this
      let formData = new FormData()
      if(file){
          formData.append('file',file.file)
      } 
      for(let i in this.paramsData){
          formData.append(i,this.paramsData[i])
      }

      if(!this.disabledFlag){  //false是新增, 
         API.POST(this.hrefUpload,formData).then(res =>{
             that.successUpload(res)
             if(!res.IsPositive){
               this.fileList=[]
             }
         })
      }else{               //true是修改
         API.PUT(this.hrefUpload,formData).then(res =>{  
            that.successUpload(res)
             if(!res.IsPositive){
               this.fileList=[]
             }
         })
      }

     },
    submitUpload(params) {
        this.paramsData=params
        if(this.fileList.length==0){
          this.$message.warning({message:this.tipMessage})
          return
        }
        if(!this.changeFlag && this.disabledFlag){       
            this.upLoadFn()
        }
        this.$refs.upload.submit()
      },
      handleBefore(){

      },
      handleProgress(){
      },
      handleRemove(file, fileList) {
        this.fileList=fileList
        this.$emit("handleChange",fileList)      //接收选着的文件图片信息
      },
      handlePreview(file) {
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择只能上传 ${this.limitUpload} 个图片`);
      },
      handleChange(file,fileList){
        this.changeFlag=true  //代表手动换了图片了
        this.fileList=fileList
        this.$emit("handleChange",fileList)  //接收选着的文件图片信息
      },
      successUpload(res){
              this.$emit("successFn",res)
      },
  },
  created() {
      if(this.fileListPro!=""){
          this.fileList.push({name:this.fileName,url:this.fileListPro})
      }
  },
  mounted() {


  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='less' scoped>
.el-upload {
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    width: 100px;
    height: 100px;
    line-height: 105px;
    vertical-align: top;
}
</style>